﻿@{
    ViewBag.Title = "客户级别";
}
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="~/Scripts/css/test.css" rel="stylesheet" />
<script src="~/Scripts/js/JQ.js"></script>
<script src="~/Scripts/global.js"></script>
<link href="~/Content/bootstrap/css/style.css" rel="stylesheet" />
<link href="~/Content/bootstrap/css/base.css" rel="stylesheet" />
<script src="~/Scripts/jquery-2.1.1.min.js"></script>
<script src="~/Scripts/layer/layer.js"></script>
<script src="~/Scripts/layPage/laypage.js"></script>
<link href="~/Scripts/css/gloabcss.css" rel="stylesheet" />
<style>
    input {
        height: 25px;
        margin-left: 15px;
    }

    .span8 input {
        width: 30%;
    }

    .container-fluid {
        margin: 0;
        padding: 0;
    }

    #edit_agency p {
        padding-top: 15px;
    }

    table tr td {
        padding: 12px 0px 12px 0px;
    }

    #Paging {
        position: absolute;
        float: right;
        left: 60%;
        margin-top: 4%;
    }
</style>
<script>
    var hiden_type = 1;
    var count =0;
    //窗体加载
    $(function () {
        $(".layui-layer-setwin").click(function () {

            Agent();
        });
        hiden_type = $("#hiden_type").val();
        Agent();
        SearchList();
        
        if (hiden_type != 1) {
            $("#btn_add").remove();
        }
    })
    //逻辑删除
    function del(obj, id) {
        layer.open({
            type: 1,
            btn: ['确定', '取消'],
            title: '删除',
            skin: "layui-layer-molv",
            area: ['200px', '135px'],
            shade: 0.3,
            content: "你确定要删除吗",
            btn1: function () {
                $.ajax({
                    type: "POST",
                    url: "/Level/delete",
                    dataType: "json",
                    data: { "id": id },
                    success: function (data) {
                        // layer.alert(JSON.stringify(data));
                        if (data.MessageCode == "0") {
                            layer.msg("删除成功！");
                            setTimeout(function () {
                                window.location.reload();
                            }, 2000)
                        } else {
                            layer.msg("删除失败！");
                        }
                    }
                });
            },
            btn2: function () {
                layer.close();
            }
        });
    }
    //查询出要编辑的人的信息
    function GetLevel(id) {
        
        $.ajax({
            type: "GET",
            url: "/Level/GetLevel",
            data: { "id": id },
            dataType: "json",
            success: function (data) {
                if (data.model.parent_id == 0) {
                    
                    $("#Jibie").hide();
                    $("#top_two").attr("style", "height:50px; margin-top:20px;")
                } else {
                    $("#Jibie").show();
                    $("#sel_level option[value='" + id + "']").remove();
                    $("#sel_level").val(data.model.parent_id);
                }
                $("#txt_name").val(data.model.name);
                $("#txt_description").val(data.model.description);
                $("#txt_coding").val(data.model.coding);
            }
        });
    }
    //添加
    function Opeas(obj,id,parentid,opea) {
        if (opea == "edit") {

            GetLevel(id);
        } else {
            Agent();
            if ($("#tab_product tbody tr").length >0)
            {
                $("#Jibie").show();
            }

        }
        var me = opea == "edit" ? "修改" : "添加";
        layer.open({
            type: 1,
            btn: ['确定', '取消'],
            title: '' + me + '类别信息',
            skin: "layui-layer-molv",
            area: ['400px', '330px'],
            shade: 0.3,
            content: $('#edit_account'),
            btn1: function () {
                if (CheckInputNullAll("edit_account")) {
                    var name = $("#txt_name").val();
                    var condig = $("#txt_coding").val();
                    var description = $("#txt_description").val();
                    var jibie = $("#sel_level").val();
                    var code = $("#sel_level option:selected").attr("code");
                    if (count != 0) {
                        if (parentid != 0) {
                            if (jibie == "" || jibie == -1) {
                                layer.msg("请选择上级级别!");
                                return false;
                            }
                        } else {
                            jibie = 0;
                        }
                    } else {
                        jibie = 0;
                    }
                    $.ajax({
                        type: "POST",
                        url: "/Level/OpeaLevel",
                        dataType: "json",
                        data: { "id": id, "opea": opea,"jibie":jibie,"name": name,"code":code,"condig":condig,"description": description },
                        success: function (data) {
                            //  layer.alert(JSON.stringify(data));
                            if (data.MessageCode == "0") {
                                layer.msg(opea == "add" ? "添加成功" : "修改成功!");
                                setTimeout(function () {
                                    window.location.reload();
                                }, 2000)
                            } else if (data.MessageCode == "50") {
                                layer.msg("信息重复!");
                            } else {
                                layer.msg(opea == "add" ? "添加失败" : "修改失败!");

                            }
                        }
                    });
                }
            },
            btn2: function () {
                Agent();
                layer.close();
            }
        })
    }
    //模糊查询
    function querylevel(pageindex, pagesize) {
        var pagecount;
        var name = $('#user_name').val();
        var mobile = $('#moblie').val();
        $.ajax({
            type: "GET",
            url: "/Level/query_level",
            data: { "pageindex": pageindex, "pagesize": pagesize },
            dataType: "json",
            async: false,
            success: function (data) {
                $("#tab_product  tr:not(:first)").remove();
                if (data.MessageCode == "0") {
                    // layer.alert(JSON.stringify(data));
                    pagecount = data.Pagecount;
                    if (data.LevelList != null) {

                        if (hiden_type == 1) {
                            $.each(data.LevelList, function (i, n) {
                                count++;
                                var html = "<tr class='rep-tab-tr1 back-f9f'>" +
                                    "<td>" + n.id + "</td>" +
                                    "<td>" + n.name + "</td>" +
                                     "<td>" + n.coding + "</td>" +
                                      "<td>" + n.description + "</td>" +
                                    "<td><input type='button' value='编辑' onclick='Opeas(this," + n.id + "," + n.parent_id + ",\"edit\")' style='width:20%;height:22px;margin-left:5px;cursor:pointer; border:none;color:#55AC58;'/><input type='button' value='删除' onclick='del(this," + n.id + ")' style='width:20%;height:22px; margin-left:5px;cursor:pointer;border:none;color:#55AC58;'/></td></tr>";
                                $("#tab_product").append(html);
                            });
                        } else {
                            $.each(data.LevelList, function (i, n) {
                                count++;
                                var html = "<tr class='rep-tab-tr1 back-f9f'>" +
                                    "<td>" + n.id + "</td>" +
                                    "<td>" + n.name + "</td>" +
                                     "<td>" + n.coding + "</td>" +
                                       "<td style=\"text-align:left;\"></td></tr>";

                                $("#tab_product").append(html);
                            });
                        }
                    } else {
                        $("#Jibie").hide();
                        $("#top_two").attr("style", "height:50px; margin-top:20px;");
                        layer.msg("暂无数据");
                    }
                } else {
                    layer.msg("查询失败，请稍后再试！");
                }
            }
        })
        return pagecount;
    }


    //分页
    function SearchList() {
        var pagecount = querylevel(1, 8);
        //好像很实用的样子，后端的同学再也不用写分页逻辑了。
        laypage({
            cont: 'pager',
            pages: pagecount, //可以叫服务端把总页数放在某一个隐藏域，再获取。假设我们获取到的是18
            skip: true, //是否开启跳页
            skin: '#5c86b2',
            curr: function () { //通过url获取当前页，也可以同上（pages）方式获取
                var page = location.search.match(/page=(\d+)/);
                return page ? page[1] : 1;
            }(),
            jump: function (e, first) { //触发分页后的回调
                if (e.pages > 1) { $('#pagercount').html('共' + e.pages + '页'); } else { $('#pagercount').hide(); }
                $('.laypage_skip').val(e.curr);
                if (!first) { //一定要加此判断，否则初始时会无限刷新
                    querylevel(e.curr, 8);
                }
            }
        });
    }
    //下拉框信息
    function Agent() {
        
        $("#sel_level").html("<option value=\"-1\" code=\"0\">请选择</option>");
        $.ajax({
            type: "GET",
            url: "/Level/GetLevelName",
            dataType: "json",
            data: {},
            success: function (data) {
                if (data.list != "") {
                    $.each(data.list, function (i, n) {
                        $("#sel_level").append("<option value='" + n.id + "' code='" + n.code + "'>" + n.name + "</option>");
                            
                    });
                  
                }
            }
        })
       
    }
</script>
<input type="hidden" value="@ViewData["hiden_type"]" id="hiden_type" />
<div class="rightoverflow">
    <div class="ove-hid">
        <div class="wid-120 cor-4d4 fon-siz20 lin-hei60 bor-botdcd ">
        </div>
    </div>
    <div class="ove-hid">
        <div class="Rep-main wid-100 mag-top50">
            <div style="">
                <input type="button" value="添加" id="btn_add" onclick="Opeas(this, 0,-1, 'add');" style=" color: white; border: none; width: 10%; height: 30px; font-weight: bold; margin-bottom: 10px; background-color: #FF8302; " />
            </div>
            <div class="pro-div" id="con_two_1">
                <table class="rep-tab wid-100 cor-4d4 tex-ali" id="tab_product">
                    <thead>
                        <tr>
                            <td>id</td>
                            <td>名称</td>
                            <td>编码</td>
                            <td>描述</td>
                            <td>操作</td>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</div>
<div id="edit_account" style="display:none;">
    <div class="divLeft" id="Jibie" style="height:50px; margin-top:20px;"> <div class="divSpan" style="line-height:25px;"><span>上级级别:</span></div>
    <div class="divInput">
        <select id="sel_level" style="margin-left: 15px;width: 85%;height: 25px;">
            <option value="-1">请选择</option>
        </select>
        </div></div>
    <div class="divLeft" style="height:50px;" id="top_two"> <div class="divSpan" style="line-height:25px;"><span>名称:</span></div><div class="divInput"><input type="text" id="txt_name" placeholder="请输入名称" class="required" /></div></div>
    <div class="divLeft" style="height:50px;"> <div class="divSpan" style="line-height:25px;"><span>代码:</span></div><div class="divInput"><input type="text" id="txt_coding" placeholder="请输入代码" class="required" style="width:70%;" />(例如A,B)</div></div>
    <div class="divLeft" style="height:50px;"> <div class="divSpan" style="line-height:25px;"><span>描述:</span></div><div class="divInput"><input type="text" id="txt_description" placeholder="请输入描述" /></div></div>
</div>

<div id="Paging">
    <span id="pagercount" style="float:left "></span>
    <div id="pager" class="pager" style="float:left">
    </div>
</div>


